import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author <a href='mailto:maruichao52@gmail.com'>MRC</a>
 * @since 2021/1/22
 */
public class TestReader {


    private static final String areaID = "600a35b2ba58b00001e4612f";
    private static final String publishUserID = "600a32356cb4fb0001fb2b5b";

    /**
     * {
     * "title": "Less变量的种类有哪些？",
     * "areaID": "5fdf56a3e2c1ee0001a52e49",
     * "publishUserID": "5ff5bdd5770c7900015b1f80",
     * "content": "",
     * "questionExplanation": [],
     * "tagID": "5ff5bc78fce5d000011cd530",
     * "createDate": "2021-01-09T14:27:23.913Z",
     * "updateDate": "2021-01-09T14:27:23.913Z",
     * "deleteDate": ""
     * }
     *
     * @param args
     */
    public static void main(String[] args) throws IOException {

        String path = "F:\\Sword\\data\\JavaGuide";
        File[] files = childrenFiles(path);

        int num = 0;

        JSONObject obj = new JSONObject();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String time = dateFormat.format(new Date());

        obj.put("areaID", areaID);
        obj.put("publishUserID", publishUserID);
        obj.put("content", "");
        obj.put("questionExplanation", new JSONArray());
        JSONArray tag = new JSONArray();
        tag.add("Java");

        obj.put("tagID", tag);
        obj.put("createDate", time);
        obj.put("updateDate", time);
        obj.put("deleteDate", "");
        obj.put("state", "onlist");

        for (File file : files) {
            if (!file.getName().endsWith(".md")) {
                continue;
            }
            File file1 = createFile(path + "\\" + file.getName().replaceAll(".md","") + ".json");
            FileReader reader = new FileReader(file);
            FileWriter writer = new FileWriter(file1);

            BufferedReader bufferedReader = new BufferedReader(reader);
            BufferedWriter bufferedWriter = new BufferedWriter(writer);

            String pattern = "^[#]{2,5} (\\d\\.)*";
            Pattern r = Pattern.compile(pattern);

            String line;
            while ((line = bufferedReader.readLine()) != null) {

                if (line.startsWith("##")) {
                    Matcher m = r.matcher(line);

                    String title = m.replaceAll("").trim();

                    System.out.println(title);

                    obj.put("title", title);

                    bufferedWriter.write(obj.toJSONString());
                    bufferedWriter.newLine();
                    bufferedWriter.flush();
                    num++;
                }
            }
            bufferedReader.close();
            bufferedWriter.close();
        }

        System.out.println(num);
    }

    public static File[] childrenFiles(String dir) {

        File file = new File(dir);

        File[] files = file.listFiles();

        return files;
    }

    public static File createFile(String path) throws IOException {

        File file = new File(path);

        if (!file.exists()) {
            file.createNewFile();
        }

        return file;
    }

}
